package com.springmvc.service;

import com.springmvc.constant.QueryConstant;
import com.springmvc.dao.UserMapper;
import com.springmvc.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Transactional(rollbackFor = Exception.class)
@Service("userService")
public class UserService{

    @Autowired
    public UserMapper userMapper;

    public User getUserById(int id) {
        return userMapper.selectByPrimaryKey(id);
    }

    public Map<String,Object> getLoginInfo(User user) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        User u = userMapper.getLoginInfo(user);
        if (u == null) {
            map.put("code", QueryConstant.FAIL_CODE);
            map.put("msg", QueryConstant.LOGIN_FAIL_REASON_UNKNOW_USER);
        } else if (!user.getPassword().equals(u.getPassword())) {
            map.put("code", QueryConstant.FAIL_CODE);
            map.put("msg", QueryConstant.LOGIN_FAIL_REASON_PASSWORD_INCORRECT);
        } else {
            map.put("code", QueryConstant.SUCCESS_CODE);
            map.put("msg", QueryConstant.LOGIN_SUCCESS);
        }
        return map;
    }
}
